home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / DR / DR_SYSV.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  4KB  |  217 lines

  1. /*************************************************************************
  2.  
  3.     dr_sysv.c       Directory (dr) subroutines
  4.             for Bywater Software.
  5.  
  6.             Implementation for AT&T Unix System V (tm)
  7.  
  8.             Copyright (c) 1991, Ted A. Campbell
  9.  
  10.             Bywater Software
  11.             P. O. Box 4023 
  12.             Duke Station 
  13.             Durham, NC  27706
  14.  
  15.             email: tcamp@hercules.acpub.duke.edu
  16.  
  17.     Copyright and Permissions Information:
  18.  
  19.     All U.S. and international copyrights are claimed by the
  20.     author. The author grants permission to use this code
  21.     and software based on it under the following conditions:
  22.     (a) in general, the code and software based upon it may be 
  23.     used by individuals and by non-profit organizations; (b) it
  24.     may also be utilized by governmental agencies in any country,
  25.     with the exception of military agencies; (c) the code and/or
  26.     software based upon it may not be sold for a profit without
  27.     an explicit and specific permission from the author, except
  28.     that a minimal fee may be charged for media on which it is
  29.     copied, and for copying and handling; (d) the code must be 
  30.     distributed in the form in which it has been released by the
  31.     author; and (e) the code and software based upon it may not 
  32.     be used for illegal activities. 
  33.  
  34. **************************************************************************/
  35.  
  36. #include "stdio.h"
  37. #ifdef    __STDC__
  38. #include "process.h"
  39. #endif
  40. #include "string.h"
  41. #include "time.h"
  42. #include "sys/types.h"
  43. #include "sys/stat.h"
  44.  
  45. #include "dr.h"
  46.  
  47. #ifndef    TRUE
  48. #define    TRUE    1
  49. #define    FALSE    0
  50. #endif
  51.  
  52. #ifndef    CR
  53. #define    CR    13
  54. #define    LF    10
  55. #endif
  56.  
  57. int    dr_fs = '/';
  58. char    dr_all[ MAX_PATHLENGTH ] = "*";
  59.  
  60. extern char *tmpname();
  61.  
  62. char tmpfname[80], command_string[80];
  63. FILE *unix_fp;
  64. static char      _path[ MAX_PATHLENGTH ];
  65.  
  66. dr_first( findb, retb )
  67.     char findb[]; 
  68.     struct dir_ent *retb;
  69.     {
  70.     register int c;
  71.     char *name;
  72.  
  73.     strcpy( tmpfname, "/tmp/dr_unix.tmp" );
  74.     sprintf( command_string, "ls -a %s | sort > %s", findb, tmpfname );
  75.     system( command_string );
  76.  
  77.     if ( ( unix_fp = fopen( tmpfname, "r" )) == NULL) 
  78.         {
  79.         bw_error( "Cannot open temporary file for directory");
  80.         return FALSE;
  81.         }
  82.  
  83.     if ( feof( unix_fp ))
  84.         {
  85.         fclose( unix_fp );
  86. #ifdef OLD_DEBUG
  87.         fprintf( "Temporary file is empty. \n" );
  88. #else
  89.         unlink( tmpfname ); 
  90. #endif
  91.         return FALSE;
  92.         }
  93.     else
  94.         {
  95.  
  96.         strcpy( _path, findb );
  97.         c = strlen( _path );
  98.  
  99.         while (( c >= 0 ) && ( _path[ c ] != dr_fs ))
  100.             {
  101. #ifdef  OLD_DEBUG
  102.             fprintf( stderr, "_path: %s c: %d\n", _path,
  103.                 c );
  104. #endif
  105.             _path[ c ] = 0;
  106.             --c;
  107.             }
  108.  
  109.         strcpy( retb->pathname, _path );
  110.  
  111.         name = fgets( command_string, 80, unix_fp );
  112.         if ( fill_buffer( retb, name ) == FALSE )
  113.             {
  114.             return FALSE;
  115.             }
  116.         else
  117.             {
  118.             return TRUE;
  119.             }
  120.         }
  121.     }
  122.  
  123. dr_next( retb )
  124.     struct dir_ent *retb;
  125.     {
  126.     char *name;
  127.  
  128.     if ( feof( unix_fp ) )
  129.         {
  130.         fclose( unix_fp );
  131. #ifndef    DEBUG
  132.         unlink( tmpfname ); 
  133. #endif
  134.         return FALSE;
  135.         }
  136.     else
  137.         {
  138.         strcpy( retb->pathname, _path );
  139.         name = fgets( command_string, 80, unix_fp );
  140.         if ( fill_buffer( retb, name )== FALSE )
  141.             {
  142.             return FALSE;
  143.             }
  144.         else
  145.             {
  146.             return TRUE;
  147.             }
  148.         }
  149.     }
  150.  
  151. fill_buffer( retb, name )
  152.     struct dir_ent *retb;
  153.     char *name;
  154.     {
  155.     static struct stat s_buf;
  156.  
  157. #ifdef OLD_DEBUG
  158.     fprintf( stderr, "fill_buffer() received filename: <%s> \n", name );
  159. #endif
  160.  
  161.     strcpy( retb->filename, name );
  162.     unix_truncate( retb->filename );
  163.  
  164.     if ( strlen( retb->filename ) == 0 )
  165.         {
  166. #ifdef OLD_DEBUG
  167.         fprintf( stderr, "Null filename returned. \n" );
  168. #endif
  169.         return FALSE;
  170.         }
  171.  
  172.     stat( retb->filename, &s_buf );
  173.  
  174.     retb->size = s_buf.st_size;
  175.  
  176.     if ( ( s_buf.st_mode & 0040000 ) == 0040000 )
  177.         {
  178.         retb->type = FILE_DIRECTORY;
  179.         }
  180.     else if ( ( s_buf.st_mode & 0000400 ) == 0000007 )
  181.         {
  182.         retb->type = FILE_EXECUTABLE;
  183.         }
  184.     else if ( ( s_buf.st_mode & 0000200 ) == 0000070 )
  185.         {
  186.         retb->type = FILE_EXECUTABLE;
  187.         }
  188.     else if ( ( s_buf.st_mode & 0000100 ) == 0000100 )
  189.         {
  190.         retb->type = FILE_EXECUTABLE;
  191.         }
  192.     else
  193.         {
  194.         retb->type = FILE_NORMAL;
  195.         }
  196.  
  197.     return TRUE;
  198.  
  199.     }
  200.  
  201. unix_truncate( buffer )
  202.     char *buffer;
  203.     {
  204.     register char *p;
  205.  
  206.     p = buffer;
  207.     while( *p != 0 )
  208.         {
  209.         if (( *p == CR ) || ( *p == LF ))
  210.             {
  211.             *p = 0;
  212.             }
  213.         ++p;
  214.         }
  215.     }
  216.  
  217.